﻿package com.dcliment.controllers
{
	import com.dcliment.display.views.WarningView;
	import com.dcliment.utils.DisplayObjectUtil;
	import com.dcliment.utils.GraphicUtil;
	import com.greensock.TweenMax;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	/**
	 * Linked to Display.views.WarningView
	 * 
	 * CODE:
	 * _timerWarning = new TimerWarningController( SPRITE );
	 * _timerWarning.tSpd = 1;
	 * _timerWarning.tEasing = "something";
	 * _timerWarning.stageHeight = 1280; // THIs is for fader;
	 * _timerWarning.steageWidth = 1080; // this is for fader
	 * _timerWarning.init(); // IMPORTANT
	 * 
	 * _timerWarning.addEventListener(TimerWarningController.YES_CLICKED, (reset application));
	 * _timerWarning.addEventListener(TimerWarningController.NO_CLICKED, (reset timer));
	 *  
	 * @author dcliment
	 * version 1.1
	 * 
	 * NOTE: trying to add this to my common library cause i use it often. Might have some issues with the "Warning" linkage cause i want to use a swc file
	 */	
	public class TimerWarningController extends EventDispatcher
	{
		public static var YES_CLICKED:String = "yes_clicked";
		public static var NO_CLICKED:String = "no_clicked";
		
		private var _view:Sprite;
		// Display ITEMS that exist in the WarningView
		private var _timeTF:TextField;
		private var _yesButton:MovieClip;
		private var _noButton:MovieClip;
		// ACCEESSORS
		private var _active:Boolean;
		private var _stageHeight:Number = 1080;
		private var _stageWidth:Number = 1920;
		// FADER properties
		private var _faderColor:uint = 0x000000;
		private var _faderAlpha:Number = 0.4;
		// Tween Properties
		private var _tSpd:Number = 0.5;
		private var _tEasing:String = "Linear.easeNone";
		
		public function TimerWarningController($view:Sprite)
		{
			_view = $view;	
			_view.visible = false;
			_view.alpha = 0;
			
		}
		public function init():void
		{
			// WARNING VIEW is attached to a MovieCLip with a linkage name "Warning" in the library
			var warningMC:WarningView = DisplayObjectUtil.getLibraryObject("Warning");
				warningMC.x = (_stageWidth / 2) - (warningMC.width / 2);
				warningMC.y = (_stageHeight / 2) - (warningMC.height / 2);
			
			// get display objects
			_yesButton 	= warningMC.yesButton;
			_noButton 	= warningMC.noButton;
			_timeTF 	= warningMC.textField;
			
			// Event Listeners	
			_yesButton.addEventListener(MouseEvent.CLICK, onYesClicked, false, 0, true);
			_yesButton.buttonMode = true;
			_noButton.addEventListener(MouseEvent.CLICK, onNoClicked, false, 0, true);
			_noButton.buttonMode = true;
			
			// FADER
			var fader:Sprite = GraphicUtil.makeRectangle(0, 0, _stageWidth, _stageHeight, _faderColor, _faderAlpha);			
			
			// Display
			_view.addChildAt(fader,0);
			_view.addChild(warningMC);
		}
		
		public function show():void
		{
			_active = true;
			
			TweenMax.to(_view, _tSpd, {autoAlpha:1, ease:_tEasing});
		}
		
		public function close():void
		{
			_active = false;
			
			TweenMax.to(_view, _tSpd, {autoAlpha:0, ease:_tEasing, onComplete:setTime, onCompleteParams:[0] });
		}
		
		public function setTime($t:int):void
		{
			_timeTF.text = String($t);
		}
		
		//----------------------------------------
		// EVENT LISTENERS
		private function onYesClicked(e:MouseEvent):void
		{
			dispatchEvent(new Event(TimerWarningController.YES_CLICKED));
			
			close();
		}
		
		private function onNoClicked(e:MouseEvent):void
		{
			dispatchEvent(new Event(TimerWarningController.NO_CLICKED));
			
			close();	
		}
		
		//----------------------------------------
		// ACCESSORS
		public function get active():Boolean
		{
			return _active;
		}
		// SET Tween properties
		public function set tSpd(value:Number):void
		{
			_tSpd = value;
		}

		public function set tEasing(value:String):void
		{
			_tEasing = value;
		}

		public function set stageHeight(value:Number):void
		{
			_stageHeight = value;
		}
		
		public function set stageWidth(value:Number):void
		{
			_stageWidth = value;
		}

		public function set faderColor(value:uint):void
		{
			_faderColor = value;
		}

		public function set faderAlpha(value:Number):void
		{
			_faderAlpha = value;
		}

		
	}
}